package com.bzy.blp.upms.web.controller;

import com.bzy.blp.upms.service.UserService;
import com.bzy.blp.upms.vo.AccountDetailVO;
import com.bzy.blp.upms.vo.AccountVO;
import com.bzy.blp.upms.vo.request.AccountRequest;
import com.bzy.common.bean.Result;
import com.bzy.common.exception.ValidateException;
import com.bzy.common.validator.Validator;
import com.bzy.common.validator.annotation.ValidateParam;
import com.bzy.shiro.util.SessionUtil;
import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户管理
 */
@RestController
@RequestMapping(value = "/userm")
public class UserMController {

    @Autowired
    UserService userService;


    /**
     * 获取后台用户列表
     * @return
     */
    @GetMapping(value = "/getAccountList")
    public Result getAccountList(String userName, String pageNo, String pageSize, HttpServletRequest request)
            throws Exception {
        Result result = new Result();

        if (pageNo == null)
            pageNo = "1";
        if (pageSize == null)
            pageSize = "20";
        PageInfo<AccountVO> accountList = userService
                .getAccountList(userName, Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        result.setData(accountList);
        return result;
    }


    /**
     * 获取后台用户详情
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/getAccountDetail")
    public Result getAccountDetail(
            @ApiParam(value = "用户id", required = true) @ValidateParam({Validator.NOT_BLANK}) String userId
            , HttpServletRequest request) throws Exception {
        Result result = new Result();
        AccountDetailVO accountDetail = userService.getAccountDetail(Long.parseLong(userId));
        result.setData(accountDetail);
        return result;
    }


    /**
     * 更新用户信息
     * @param accountRequest
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping(value = "updateAccountInfo")
    public Result updateAccountInfo(AccountRequest accountRequest, HttpServletRequest request) throws Exception {
        if (accountRequest == null) {
            throw new ValidateException("用户信息不能为空");
        }
        Result result = new Result();
        String uptPeople = (String) SessionUtil.getPrincipal();
        accountRequest.setOperatePeople(uptPeople);
        userService.updateAccountInfo(accountRequest);
        return result;
    }


    /**
     * 新增
     * @param accountRequest
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping(value = "addAccountInfo")
    public Result addAccountInfo(AccountRequest accountRequest, HttpServletRequest request) throws Exception {
        if (accountRequest == null) {
            throw new ValidateException("新增用户信息不能为空");
        }
        Result result = new Result();
        userService.addAccountInfo(accountRequest);
        return result;
    }


    /**
     * 重置用户密码
     * @param userId
     * @param newPassword
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping(value = "resetUserPassword")
    public Result resetUserPassword(
            @ApiParam(value = "用户id", required = true) @ValidateParam({Validator.NOT_BLANK}) String userId,
            @ApiParam(value = "新密码", required = true) @ValidateParam({Validator.NOT_BLANK}) String newPassword,HttpServletRequest request) throws Exception {
        Result result = new Result();

        return result;
    }


    /**
     * 修改当前密码
     * @param oldPassword
     * @param newPassword
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping(value = "changeOwnerPassword")
    public Result changeOwnerPassword(
            @ApiParam(value = "旧密码", required = true) @ValidateParam({Validator.NOT_BLANK}) String oldPassword,
            @ApiParam(value = "新密码", required = true) @ValidateParam({Validator.NOT_BLANK}) String newPassword, HttpServletRequest request) throws Exception {
        Result result = new Result();

        return result;
    }
}
